package com.yiyuan.system.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.system.model.Employee;
import com.yiyuan.system.model.Organ;
import com.yiyuan.system.service.ISystemService;

public class EmpAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    /* **** 分页参数封装********** */
    private String startrecord;
    private String endrecord;
    private String perpage;
    /* **** form参数************* */
    private String empId;
    private String empName;
    private String empNo;
    private String deptId;
    /* **** 输出结果封装*********** */
    private Employee employee;
    private List<Employee> list;
    private List<Organ> deptList;
    /* **** 资源引用************** */
    @Resource
    private ISystemService systemService;
    
    /** ****************************action方法*********************************** */
    // 进入人员主页
    public String index() {
        this.deptList = this.systemService.findOrgList();
        return "toMain";
    }
    
    // 保存人员信息
    public void save() throws Exception {
        Employee employee = null;
        if (this.empId != null && !this.empId.equals("")) {// 修改
            employee = this.systemService.loadEmp(Integer.valueOf(this.empId));
            employee.setEmpName(this.getEmpName());
            employee.setDeptId(Integer.valueOf(this.deptId));
            systemService.updateEmp(employee);
        } else {// 新增
            employee = new Employee();
            employee.setDeptId(Integer.valueOf(this.deptId));
            employee.setEmpNo(this.empNo);
            employee.setEmpName(this.empName);
            this.systemService.addEmp(employee);
        }
        
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().setContentType("text/xml; charset=utf-8");
        ServletActionContext.getResponse().getWriter().println("1");
        
    }
    
    // 人员列表分页显示
    @SuppressWarnings("unchecked")
    public void listPage() throws Exception {
        
        int start = 1;
        int end = 100;
        
        // 三个内置分页参数
        if (this.getStartrecord() != null && this.getEndrecord() != null && this.getPerpage() != null) {
            start = Integer.parseInt(this.getStartrecord());
            end = Integer.parseInt(this.getEndrecord());
        }
        // 获得页数及每页记录数
        Integer pDeptId = null;
        if (this.deptId != null && !this.deptId.equals("")) {
            pDeptId = Integer.valueOf(this.deptId);
        }
        int totalRecord = systemService.getEmpPageCount(this.empName, pDeptId);
        list = systemService.findEmpList(start, end, empName, pDeptId);
        
        // 封装输出结果
        Map map = new HashMap();
        map.put("list", list);
        map.put("totalRecord", totalRecord);
        JSONObject jso = new JSONObject(map);
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().setContentType("text/xml; charset=utf-8");
        ServletActionContext.getResponse().getWriter().print(jso.toString());
        
    }
    
    // 删除人员
    public void del() throws Exception {
        String ret = null;
        try {
            this.systemService.deleteEmp(Integer.valueOf(this.empId));
            ret = "delete";
        } catch (Exception e) {
            ret = "error";
        }
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().setContentType("text/xml; charset=utf-8");
        ServletActionContext.getResponse().getWriter().print(ret);
        
    }
    
    // 查看人员详细
    public String view() {
        if (this.empId != null && !this.empId.equals("")) {
            this.employee = this.systemService.loadEmp(Integer.valueOf(this.empId));
        }
        return "toView";
        
    }
    
    //进入人员新增页面
    public String toAdd() {
        return "toAdd";
    }
    
    //根据部门查询用户
    @SuppressWarnings("unchecked")
    public void getEmpList() throws IOException {
        
        List<Employee> empList = systemService.getEmpListByDeptId(Integer.valueOf(deptId));
        Map map = new HashMap();
        map.put("list", empList);
        
        JSONObject jso = new JSONObject(map);
        
        ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
        ServletActionContext.getResponse().setContentType("text/xml; charset=utf-8");
        ServletActionContext.getResponse().getWriter().print(jso.toString());
        
    }
    
    /** ****************************action方法************************************ */
    public List<Employee> getList() {
        return list;
    }
    
    public Employee getEmployee() {
        return this.employee;
    }
    
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    
    public String getStartrecord() {
        return startrecord;
    }
    
    public void setStartrecord(String startrecord) {
        this.startrecord = startrecord;
    }
    
    public String getEndrecord() {
        return endrecord;
    }
    
    public void setEndrecord(String endrecord) {
        this.endrecord = endrecord;
    }
    
    public String getPerpage() {
        return perpage;
    }
    
    public void setPerpage(String perpage) {
        this.perpage = perpage;
    }
    
    public String getEmpId() {
        return empId;
    }
    
    public void setEmpId(String empId) {
        this.empId = empId;
    }
    
    public String getEmpName() {
        return empName;
    }
    
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    
    public String getEmpNo() {
        return empNo;
    }
    
    public void setEmpNo(String empNo) {
        this.empNo = empNo;
    }
    
    public String getDeptId() {
        return deptId;
    }
    
    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }
    
    public List<Organ> getDeptList() {
        return deptList;
    }
    
    public void setDeptList(List<Organ> deptList) {
        this.deptList = deptList;
    }
    
}
